import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import javax.swing.border.Border;

/**
 * Copyright (C) 2013  Arun Lee & Bryce Nagareda
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Creates horizontal sundial
 * @author Arun Lee
 */
public class HSundialMath {
	
	private int latitude;
	private String timezone;
	private double[] hourlines;
	private boolean south = false;
	//created for printing purpose
	private DrawHDialFace drawF;
	private DrawHGnomon drawG;
	private int radius = 250;
	/**
	 * create a HSunidalMath to initial all the parts for Horizontal Sundial
	 * @param lat latiude
	 * @param lon longitude
	 * @param tz time zone
	 * @param hrl hour lines
	 */
	public HSundialMath(int lat, int lon, String tz, double[] hrl){
		this.latitude = lat;
		this.timezone = tz;
		this.hourlines = hrl;
		if (this.latitude < 0){
			this.south = true;
		}
		HSundialComponent();
	}
	/**
	 * creates the interface for user
	 */
	public void HSundialComponent(){
		JFrame frame = new JFrame("Horizontal Sundial");
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		//add a tabbed pane to show the gnomon and dial face
		JTabbedPane tp = new JTabbedPane();
		this.drawG = new DrawHGnomon(Math.abs(this.latitude), this.timezone, this.south, this.radius);
		this.drawF = new DrawHDialFace(this.hourlines, this.south, this.timezone, this.radius);
		tp.addTab("Gnomon", this.drawG);
		tp.addTab("Dial", this.drawF);
		frame.getContentPane().add(tp, BorderLayout.CENTER);
		
		//Add instruction
		JPanel p1 = new JPanel();
		JTextArea text = new JTextArea();
		Border mb = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.blue.brighter());
		p1.setBorder(mb);
		text.setFont(new Font("Arial", Font.BOLD, 15));
		text.setText("Instruction to build the horizontal sundial:\n"
				+ "\n-  print both gnomon and dial face"
				+ "\n-  cut out the gnomon (triangle piece)"
				+ "\n-  tape it along the red line on dial face"
				+ "\n-  TADA! you just made a Horizontal sundial "
				+ "\n\n\n Now take it out and face direction indicated " 
				+  "\n and see what time is it!");
		text.setEditable(false);
		p1.add(text);
		p1.setPreferredSize(new Dimension(400, 400));
		p1.setBackground(Color.white);
		frame.add(p1, BorderLayout.WEST);
		
		//add a print button
		JPanel p2 = new JPanel();
		JButton print = new JButton("Print");
		print.addActionListener(new PrintButtonListener());
		p2.add(print, BorderLayout.CENTER);
		p2.setBackground(Color.white);
		p2.setPreferredSize(new Dimension(400, 50));
		frame.add(p2, BorderLayout.SOUTH);
		
		frame.pack();
		frame.setVisible(true);
	}
	/**
	 * accessor method for testing
	 * @return latitude
	 */
	public int getlatitude(){
		return this.latitude;
	}
	/**
	 * accessor method for testing
	 * @return longitude
	 */
	public int getlongitude(){
		return this.latitude;
	}
	/**
	 * accessor method for testing
	 * @return hour lines
	 */
	public double[] getHourlines(){
		return this.hourlines;
	}
	/**
	 * accessor method for testing
	 * @return true if south
	 */
	public boolean getSouth(){
		return this.south;
	}
	/**
	 * Action listener for print button
	 * @author Arun
	 *
	 */
	private class PrintButtonListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			print();
		}
	}
	
	/**
	 * open print dialog and enable printing activity
	 */
	private void print(){
		PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        pf.setOrientation(PageFormat.LANDSCAPE);
        //send DrawDialFace to printing 
        job.setPrintable(this.drawF, pf);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            	JOptionPane.showMessageDialog(null, "There was a problem printing");
            }
        }
        //send DrawGnomon to printing
        job.setPrintable(this.drawG, pf);
        ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            	JOptionPane.showMessageDialog(null, "There was a problem printing");
            }
        }
	}
	
	/**
	 * Simple test to see how the gui looks
	 * @param args not used
	 */
	public static void main(String[] args){
		SundialMath test = null;
		try {
			test = new SundialMath(64.091408, -154.467773, 15, "April");
			//test = new SundialMath(21.453069, -158.016357, 15, "April");
		} catch (MonthDayException e) {
			e.printStackTrace();
		}
		if (test != null) {
			HSundialMath test1 = new HSundialMath((test.getLatitude()), (test.getLongitude()), test.getTimeZoneName(), test.getHourLines());
		}
	}
}

